package com.tender.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tender.page.PageDTO;
import com.tender.page.Pagination;
import com.tender.condition.SysMsgInfoCondition;
import com.tender.entity.SysMsgInfoEntity;
import com.tender.enums.OnlineEnums;
import com.tender.mapper.SysMsgInfoMapper;
import com.tender.service.SysMsgInfoService;
import com.tender.utils.LoginUser;
import com.tender.utils.UserProvider;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysMsgInfoServiceImpl extends ServiceImpl<SysMsgInfoMapper, SysMsgInfoEntity> implements SysMsgInfoService {

    @Override
    public SysMsgInfoEntity detail(Long id) {
        return getBaseMapper().selectById(id);
    }

    @Override
    public IPage<SysMsgInfoEntity> pageList(SysMsgInfoCondition condition) {
        Pagination pagination = condition.getPagination();
        Page<SysMsgInfoEntity> page = pagination.toPage();
        QueryWrapper<SysMsgInfoEntity> queryWrapper = new QueryWrapper<>();
        if (null != condition.getOnline()) {
            queryWrapper.lambda().eq(SysMsgInfoEntity::getOnline, condition.getOnline());
        }
        queryWrapper.lambda().eq(null != condition.getOnline(), SysMsgInfoEntity::getClassification, condition.getOnline())
                .eq(null != condition.getClassification(), SysMsgInfoEntity::getClassification, condition.getClassification())
                .like(StringUtils.isNotBlank(condition.getTitleLike()), SysMsgInfoEntity::getTitle, condition.getTitleLike());
        return this.page(page, queryWrapper);
    }

    /**
     * 手写的分页
     */
    @Override
    public IPage<SysMsgInfoEntity> pageListUser(SysMsgInfoCondition condition) {
        Pagination pagination = condition.getPagination();
        LoginUser userInfo = UserProvider.getUserInfo();
        // 算总数
        int total = this.getBaseMapper().customCount(userInfo.getUserId());
        if (0 == total) {
            return PageDTO.emptyPageEntity(pagination);
        }

        // 查询列表
        List<SysMsgInfoEntity> sysMsgInfoEntities = this.getBaseMapper().pageListUser(userInfo.getUserId(),
                (int) ((pagination.getCurrentPage() - 1) * pagination.getPageSize()),
                (int) pagination.getPageSize());
        Page<SysMsgInfoEntity> page = new Page<>();
        page.setRecords(sysMsgInfoEntities);
        page.setTotal(total);
        page.setCurrent(pagination.getCurrentPage());
        page.setSize(pagination.getPageSize());

        return page;
    }

    @Override
    public Boolean online(Long id) {
        UpdateWrapper<SysMsgInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMsgInfoEntity::getId, id)
                .set(SysMsgInfoEntity::getOnline, OnlineEnums.YES.getCode());
        return getBaseMapper().update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean offline(Long id) {
        UpdateWrapper<SysMsgInfoEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.lambda().eq(SysMsgInfoEntity::getId, id)
                .set(SysMsgInfoEntity::getOnline, OnlineEnums.NO.getCode());
        return getBaseMapper().update(null, updateWrapper) > 0;
    }

    @Override
    public Boolean update(SysMsgInfoEntity param) {
        return getBaseMapper().updateById(param) > 0;
    }
}
